package gradingcalculator;

/**
 *
 * @author Marvin
 */
public class User {
    
    //Attributes
    private String name;
    private String title;
    private String school;
    private boolean gender; //male = true, female = false
    private Term[] terms;
    
    //Constructor
    public User(String name, String title, String school, boolean gender){
        this.name = name;
        this.title = title;
        this.school = school;
        this.gender = gender;
    }
    
    public User(String name){
        this.name = name;
    }
    
    //Returns
    public String getName(){
        return name;
    }
    public String getTitle(){
        return title;
    }
    public String getSchool(){
        return school;
    }
    public boolean getGender(){
        return gender;
    }
    public Term[] getTerms(){
        return terms;
    }
    public Term getTerm(int index){
        return terms[index];
    }
    
    //Modifiers
    public void setName(String name){
        this.name = name;
    }
    public void setTitle(String title){
        this.title = title;
    }
    public void setSchool(String school){
        this.school = school;
    }
    public void setGender(boolean gender){
        this.gender = gender;
    }
    public void setTerms(Term[] terms){
        this.terms = terms;
    }
    public void setTerm(Term term, int index){
        this.terms[index] = term;
    }
    
// ===== Advanced Modifiers (Adds, Counters, and Array Manipulators)============
    
    //ADDS
    public void addTerm(String name){
        //Create an element in the array if the array is null
        if(terms == null) { terms = new Term[1]; }
        //Otherwise add another element to the end of array
        else { expandTermsBy(1); }
        //Create a Term in the Grading Calculator named 'name parameter'
        terms[terms.length-1] = new Term(name);
    }
    
    //ARRAY MANIPULATOR
    private void expandTermsBy(int i) {
        Term[] proxy = new Term[terms.length+i];
        System.arraycopy(terms, 0, proxy, 0, terms.length);
        terms = proxy;
    }
    
// ===== Advanced Modifiers (Grade Calculations)================================
    
    // This method Counts how many 'instances' of the 'item' are 'graded'.
    public int countGraded(Item item){
        int count = 0;
        for(int i=0; i<item.getInstances().length; i++){
            if(item.getInstance(i).ifScored()){
                count +=1;
            }
        }
        return count;
    }
    
    // This method sums all the 'scores marked' of each 'instance' in the given 'item'.
    public double sumScoreMarked(Item item){
        double sum = 0;
        for(int i = 0; i<item.getInstances().length; i++){
            sum+=item.getInstance(i).grade();
        }
        return sum;
    }
    
    // This method sums all the 'maximum scores' of each 'graded instance' in the given 'item'.
    public double sumScoreGraded(Item item) {
        double sum = 0;
        for(int i = 0; i<item.getInstances().length; i++){
            if(item.getInstance(i).ifScored()){
                sum+=item.getInstance(i).grade();
            }
        }
        return sum;
    }
    
    // This method sums all the 'maximum scores' of all 'instance' in the given 'item'.
    public double sumScoreMax(Item item) {
        double sum = 0;
        for(int i = 0; i<item.getInstances().length; i++){
            sum+=item.getInstance(i).max();
        }
        return sum;
    }
    
    // This method calculates the 'grade' of all the 'graded scores' in the item.
    public double calcGrade(Item item) {
        double grade;
        grade = sumScoreMarked(item)/sumScoreMarked(item);
        return grade;
    }
    
    // This method calculates the 'grade' of all the 'scores including ungraded' in the item.
    public double calcGradeMax(Item item) {
        double grade;
        grade = sumScoreMarked(item)/sumScoreMarked(item);
        return grade;
    }
    
    // This method calculates the 'grade' of all the 'graded scores' in the subject.
    public double calcGrade(Subject subject) {
        double grade = 0;
        for(int i = 0; i < subject.getItems().length; i++){
            Item item = subject.getItem(i);
            grade+=(calcGrade(item)*item.getWeight()) / 100.00;
        }
        return grade;
    }
    
    // This method calculates the 'grade' of all the 'scores including ungraded' in the subject.
    public double calcGradeMax(Subject subject){
        double grade = 0;
        for(int i = 0; i < subject.getItems().length; i++){
            Item item = subject.getItem(i);
            grade+=(calcGradeMax(item)*item.getWeight()) / 100.00;
        }
        return grade;
    }
    
    
    public char calcLetterGrade(double grade){
        char letterGrade;
        if(grade >= 90.0){
            letterGrade = 'A';
        }
        else{
            if (grade >= 80.0){
                letterGrade = 'B';
            }
            else{
                if (grade >= 70.0){
                    letterGrade = 'C';
                }
                else{
                    letterGrade = 'F';
                }
            }
        }
        return letterGrade;
    }
    
}
